Socket
Socket
Sign inDemoInstall

power-di

Package Overview
Dependencies
Maintainers
1
Versions
121
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

power-di

A lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.


Version published
Weekly downloads
488
increased by24.17%
Maintainers
1
Weekly downloads
 
Created
Source

Power DI

CI Coverage Version License

A lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.

Install

npm i power-di --save

Example

a simple example

import { IocContext } from 'power-di'

class AService { }

// default instance, you can also new IocContext to get a instance.
const context = IocContext.DefaultInstance
context.register(AService)
const aService = context.get(AService) // a instance of AService

inject with key

class AService { }

const context = IocContext.DefaultInstance
context.register(AService, 'XService') // key need a string or class, e.g super class or whatever class.
context.get('XService')

use with decorators

import { getDecorators } from '../helper'
const {
    register, append, inject, lazyInject, registerSubClass, lazyInjectSubClass
} = getDecorators() // you can provider a iocContext for this method.

@register()
class AService { }

class SomeClass {
    @inject() // set right now
    private aService: AService

    @lazyInject() // set when using
    private bService: AService
}

context.get(AService)

use in react

import { IocProvider, Component } from 'power-di/react'

@register()
class AService { }

class TestComponent extends Component<{}, {}> {
    componentWillMount() {
        this.GetComponent(AService)
    }

    render(): any {
        return null
    }
}

// parent component, the IocProvider is not necessary.
<IocProvider context={context}>
    <TestComponent />
</IocProvider>

collect some kind of object

class A { }

@register(undefined, { regInSuperClass: true })
class B extends A { }

@registerSubClass() // the abbreviation of above
class C extends A { }

@append(A)
class D { }

class LITestService {
    @lazyInject({ type: A, subClass: true }) // need type when inject subClass, Reflect cannot get the type.
    public testService1: A[] // [b, c, d], the type is A[] or any[] (D may be not instance of A)

    @lazyInjectSubClass({ type: A }) // need type when inject subClass, Reflect cannot get the type.
    public testService2: A[] // the abbreviation of above
}
See the test case for details.

Keywords

FAQs

Package last updated on 26 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc